package com.jy.rhin.domain.document.node;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jy.rhin.infra.repo.node.po.NodeAuthenticationPermissionPo;
import com.jy.rhin.infra.repo.node.po.NodeAuthenticationPermissionRelationPo;
import com.jy.rhin.sdk.dto.node.NodeAuthenticationPermissionDto;
import com.jy.rhin.sdk.dto.node.NodeAuthenticationPermissionModuleDto;
import com.jy.rhin.sdk.dto.node.NodeAuthenticationPermissionRelationDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@Service
@Slf4j
public class NodeAuthenticationPermissionService {

    @Autowired
    private NodeAuthenticationPermissionDao nodeAuthenticationPermissionDao;

    @Autowired
    private NodeAuthenticationPermissionRelationDao nodeAuthenticationPermissionRelationDao;
    


   public List<NodeAuthenticationPermissionModuleDto> getNodePermissionList(){
       List<NodeAuthenticationPermissionModuleDto> resultList = new ArrayList<>();
       List<NodeAuthenticationPermissionPo> permissionList = nodeAuthenticationPermissionDao.getAllPermission();
      Map<String,List<NodeAuthenticationPermissionPo>> permissionMap = permissionList.stream().collect(Collectors.groupingBy(NodeAuthenticationPermissionPo::getPermissionModule));
       permissionMap.forEach((s,v)->{
           NodeAuthenticationPermissionModuleDto nodeAuthenticationPermissionModuleDto = new NodeAuthenticationPermissionModuleDto();
           nodeAuthenticationPermissionModuleDto.setPermissionModule(s);
           nodeAuthenticationPermissionModuleDto.setNodeAuthenticationPermissionDtoList(toDtoList(v));
           resultList.add(nodeAuthenticationPermissionModuleDto);
       });
       return resultList;
    }

    public List<NodeAuthenticationPermissionRelationDto> getPermissionListByNodeId(Long nodeId){
        return toRelationDtoList(nodeAuthenticationPermissionRelationDao.getPermissionListByNodeId(nodeId));
    }

    public void saveNodePermissionRelation(List<NodeAuthenticationPermissionRelationDto> nodeAuthenticationPermissionRelationDtoList){
        nodeAuthenticationPermissionRelationDao.save(toRelationPoList(nodeAuthenticationPermissionRelationDtoList));
    }


    private NodeAuthenticationPermissionDto toDto(NodeAuthenticationPermissionPo po) {
        NodeAuthenticationPermissionDto dto = new NodeAuthenticationPermissionDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

    private List<NodeAuthenticationPermissionDto> toDtoList(List<NodeAuthenticationPermissionPo> poList) {
        List<NodeAuthenticationPermissionDto> dtoList=new ArrayList<>();
        if(!CollectionUtils.isEmpty(poList)){
            poList.forEach(po->{
                NodeAuthenticationPermissionDto dto = new NodeAuthenticationPermissionDto();
                BeanUtils.copyProperties(po, dto);
                dtoList.add(dto);
            });
        }
        return dtoList;
    }

    private List<NodeAuthenticationPermissionRelationPo> toRelationPoList(List<NodeAuthenticationPermissionRelationDto> dtoList) {
        List<NodeAuthenticationPermissionRelationPo> poList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(dtoList)){
            dtoList.forEach(dto->{
                NodeAuthenticationPermissionRelationPo po = new NodeAuthenticationPermissionRelationPo();
                BeanUtils.copyProperties(dto, po);
                poList.add(po);
            });
        }

        return poList;
    }

    private List<NodeAuthenticationPermissionRelationDto> toRelationDtoList(List<NodeAuthenticationPermissionRelationPo> poList) {
        List<NodeAuthenticationPermissionRelationDto> dtoList=new ArrayList<>();
        if(!CollectionUtils.isEmpty(poList)){
            poList.forEach(po->{
                NodeAuthenticationPermissionRelationDto dto = new NodeAuthenticationPermissionRelationDto();
                BeanUtils.copyProperties(po, dto);
                dtoList.add(dto);
            });
        }
        return dtoList;
    }
    
    public Boolean hasPermission(String publicKey,String action){
    	return nodeAuthenticationPermissionRelationDao.getPermissionByPkAndCode(publicKey, action)>0;
    }

}
